﻿
using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using System.Collections;
using Components;
using Model;
using MsDAL;
using System.ComponentModel;
using ServiceHelper.Doc;

namespace ServiceHelper
{
    [DataObjectAttribute]
    public class OrdSendManager
    {
        private Ord3a00DAL dal = new Ord3a00DAL();

        private static OrdSendManager _entity;

        public static OrdSendManager Entity
        {
            get
            {
                if (_entity == null)
                    _entity = new OrdSendManager();
                return _entity;
            }
        }

        #region 个人代码
        public string SaveData(Hashtable tb, DataTable dtl, DataTable deldata, string action, string usercode)
        {
            List<SqlApp> lst = new List<SqlApp>();

            if ("A" == action && (dtl == null || dtl.Rows.Count == 0)) return "明细中没有数据，不需要保存！";

            if ("A" == action) //发货
            {
                //表头
                var helper = new SendManager();
                Sto8a00 sto8A00 = null;

                try
                {
                    sto8A00 = helper.ParseDoc(tb);
                    helper.Parse(sto8A00);

                    foreach (DataRow row in dtl.Rows)
                        helper.ParseRow(row);

                    var msg = helper.Save();

                    if (!string.IsNullOrEmpty(msg))
                        return msg;
                }
                catch (Exception ex)
                {
                    return ex.Message;
                }

                return Json.ToString(new
                                         {
                                             status = true,
                                             id = sto8A00.Listid
                                         });

            }
            else if ("P" == action) //更新打印发货单
            {
                lst.Add(dal.AlreadyPrint(Components.Validation.ToString(tb["listid"])));
            }
            else if ("U" == action) //不需要打印发货单，直接更新为已发货
            {
                lst.Add(dal.AlreadySendNonePrint(Components.Validation.ToString(tb["listid"])));
            }
            else if ("X" == action) //打印配货单
            {
                lst.Add(dal.AlreadyPrintPreDoc(Components.Validation.ToString(tb["listid"])));
            }
            else if ("B" == action) //回单录入
            {
                foreach (DataRow dr in dtl.Rows)
                {
                    lst.Add(dal.BackConfirm(Components.Validation.ToString(dr["listid"]),
                        Components.Validation.ToDouble(dr["accqty"]),
                        Components.Validation.ToDouble(dr["accpreqty"]),
                        Components.Validation.ToDouble(dr["accamt"])
                        ));
                }

                string remark = Components.Validation.ToString(tb["accremark"]);
                double accamt = Components.Validation.ToDouble(tb["accamt"]);

                //更新订单状态(将订单状态更新为回单已经确认)
                lst.Add(dal.AlreadyBack(Components.Validation.ToString(tb["listid"]),
                    Components.Validation.ToString(tb["backno"]), remark, accamt));
            }
            else if ("W" == action) //发货确认
            {
                lst.Add(dal.UpdateSendStatus(Components.Validation.ToString(tb["listid"])));
            }

            return CommonManager.Entity.SaveData(lst);
        }

        #endregion
    }
}



